package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizUploadQueueLog;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizUploadQueueLogMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizUploadQueueLogParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizUploadQueueLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;

/**
 * 网关上报分数表 服务实现类
 *
 * @author ssy
 * @date 2021/11/02 18:06
 */
@Service
public class BizUploadQueueLogServiceImpl extends ServiceImpl<BizUploadQueueLogMapper, BizUploadQueueLog> implements BizUploadQueueLogService {

    @Override
    public PageResult<BizUploadQueueLog> page(BizUploadQueueLogParam bizUploadQueueLogParam) {

        // 构造条件
        LambdaQueryWrapper<BizUploadQueueLog> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotNull(bizUploadQueueLogParam)){
            if(ObjectUtil.isNotEmpty(bizUploadQueueLogParam.getGatewayId())){
                queryWrapper.eq(BizUploadQueueLog::getGatewayId,bizUploadQueueLogParam.getGatewayId());
            }
            if(ObjectUtil.isNotEmpty(bizUploadQueueLogParam.getGatewayChannel())){
                queryWrapper.eq(BizUploadQueueLog::getGatewayChannel,bizUploadQueueLogParam.getGatewayChannel());
            }
        }

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<BizUploadQueueLog> list(BizUploadQueueLogParam bizUploadQueueLogParam) {

        // 构造条件
        LambdaQueryWrapper<BizUploadQueueLog> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(BizUploadQueueLogParam bizUploadQueueLogParam) {

        // 将dto转为实体
        BizUploadQueueLog bizUploadQueueLog = new BizUploadQueueLog();
        BeanUtil.copyProperties(bizUploadQueueLogParam, bizUploadQueueLog);

        this.save(bizUploadQueueLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizUploadQueueLogParam bizUploadQueueLogParam) {
        this.removeById(bizUploadQueueLogParam.getId());
    }

    @Override
    public void edit(BizUploadQueueLogParam bizUploadQueueLogParam) {

        // 根据id查询实体
        BizUploadQueueLog bizUploadQueueLog = this.queryBizUploadQueueLog(bizUploadQueueLogParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(bizUploadQueueLogParam, bizUploadQueueLog);

        this.updateById(bizUploadQueueLog);
    }

    @Override
    public BizUploadQueueLog detail(BizUploadQueueLogParam bizUploadQueueLogParam) {
        return this.queryBizUploadQueueLog(bizUploadQueueLogParam);
    }

    @Override
    public void addAndReturnId(BizUploadQueueLogParam bizUploadQueueLogParam) {
        this.baseMapper.addAndReturnId(bizUploadQueueLogParam);
    }

    @Override
    public PageResult<Map<String,Object>> pageList(BizUploadQueueLogParam uploadQueueLogParam) {
        String[] list = new String[1];
        String gatewayIds = uploadQueueLogParam.getGatewayIds();
        if(StringUtils.isNotBlank(gatewayIds)){
            list = gatewayIds.split(",");
        }else {
            list[0] = "";
        }
        return new PageResult<>(this.baseMapper.listMap(PageFactory.defaultPage(),uploadQueueLogParam,list));
    }


    /**
     * 获取网关上报分数表
     *
     * @author ssy
     * @since 2021/11/02 18:06
     */
    private BizUploadQueueLog queryBizUploadQueueLog(BizUploadQueueLogParam bizUploadQueueLogParam) {
        BizUploadQueueLog bizUploadQueueLog = this.getById(bizUploadQueueLogParam.getId());
        return bizUploadQueueLog;
    }
}
